<!--  -->
<template>
  <div class="">
    <el-card>
      <el-form :model="categoryDatas" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类级别">
              <el-cascader
                v-model="value"
                :options="options"
                :props="{ checkStrictly: true }"
                @change="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称">
              <el-input
                v-model="categoryDatas.title"
                placeholder="产品名称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品图片">
              <el-upload
                class="avatar-uploader"
                action="https://jsonplaceholder.typicode.com/posts/"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否可用">
              <el-switch
                v-model="categoryDatas.is_show"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="可用"
                inactive-text="禁用"
                active-value="1"
                inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="">
              <el-button type="primary">添加产品</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  //ComponentName和文件名保持一致
  name: "ComponentName",
  data() {
    //这里存放数据
    return {
      value: [],
      imageUrl: "",
      options: [
        {
          value: "1",
          label: "家用电器",
          children: [
            {
              value: "101",
              label: "大家电",
              children: [
                {
                  value: "10001",
                  label: "电视",
                },
                {
                  value: "10002",
                  label: "冰箱",
                },
              ],
            },
            {
              value: "102",
              label: "厨房电器",
            },
          ],
        },
      ],
      categoryDatas: {
        title: "",
        img: "",
        is_show: "",
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    handleChange(value) {
      console.log(value);
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  deactivated() {}, //如果页面有keep-alive缓存功能，当组件被移除时，触发钩子函数deactivated
};
</script>
<style>
</style>